#include "chat.h"
#include "ui_chat.h"
#include "main.h"

Chat::Chat(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Chat)
{
    ui->setupUi(this);
    hb_timer = new HeartBTimer();//msgthread
    ui->user_name->setText(QString::fromStdString(msgthread->GetName()));
    connect(msgthread, SIGNAL(MsgComing(string,string)), this, SLOT(DealMsgComing(string, string)));
    connect(msgthread, SIGNAL(HeartBRSP(string)), this, SLOT(DealHeartBRSP(string)));
}

Chat::~Chat()
{
    delete ui;
}

void Chat::on_btn_sendmsg_clicked()
{

//    SockSendMsg(CreateMessageMsg())
    //转发给用户;
    TransMsg trans_msg;
    if (btngroup_user.checkedButton()){
        cout << "checkedbtn text: " << btngroup_user.checkedButton()->text().toStdString() << endl;
        CreateMessageMsg(trans_msg, msgthread->GetName().c_str(), btngroup_user.checkedButton()->text().toStdString().c_str(),
                         ui->textedit_sendmsg->toPlainText().toStdString().c_str());
        SockSendMsg((char *)&trans_msg, send_client);
        string str =  "i say:\n" +  ui->textedit_sendmsg->toPlainText().toStdString() + "\n\n";
        QString tmp = ui->textview_chat->toPlainText();
        tmp += QString::fromStdString(str);
        ui->textview_chat->setText(tmp);
    }else {
        QMessageBox::information(nullptr, "SEND", "choose who you want to send");
        cout << "choise btn!!!";
    }

//    SockSendMsg(CreateMessageMsg(my_name, "123456", "hello"), send_client);
}


//void Chat::on_btn_exit_clicked()
//{
//    this->close();
//    delete hb_timer;
//    Login *win_log = new Login();
//    win_log->show();
//}

void Chat::DealMsgComing(string from, string content){
    string str =  from + ":\n" + content + "\n\n";
    QString tmp = ui->textview_chat->toPlainText();
    tmp += QString::fromStdString(str);
    ui->textview_chat->setText(tmp);
}

void Chat::DealHeartBRSP(string content){
    cout << content << endl;
    SplitRspHeartbMsgContent(live, content);

    refresh();
//    QMessageBox::information(nullptr,
//                             "Message",
//                             QString::fromStdString(content));
}

void Chat::refresh(){
    size_t lsize = live.size();
    if (lsize > btngroup_user.buttons().size()){
        //添加按钮
        static int btn_id = 0;
        cout << "compare: " << lsize << ": " << btngroup_user.buttons().size();
        while (btngroup_user.buttons().size() < lsize){
            cout << "compare: " << lsize << ": " << btngroup_user.buttons().size();
            Btn_user *tmp = new Btn_user();
            ui->list_users_2->addWidget(tmp);
            btngroup_user.addButton(tmp->GetBtn(), btn_id++);
        }
    }else{

    }
    int i = 0;
    for (auto element : live) {
        btngroup_user.button(i)->setText(QString::fromStdString(element.first));
        if (element.second)btngroup_user.button(i)->show();
        else btngroup_user.button(i)->hide();
        i++;
    }
//    for (auto element : live){

//    }
//    btngroup_user.buttons().size();
//    for(auto element : live){

//    }
    cout << "refresh" << endl;
}
